package com.kabu.aqs;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author jiawuwei
 * @Copyright: Copyright (c) 兆日科技股份有限公司  2020
 * @date 2020/8/3,10:22
 */
public class ReentrantLockTest {
    public static void main(String[] args) throws Exception {
        AddDemo runnalbeDemo = new AddDemo();
        //Thread thread = new Thread(runnalbeDemo::add);
        //thread.start();
        //Thread thread1 = new Thread(runnalbeDemo::add);
        //thread1.start();
        Thread.sleep(1000);
        System.out.println(runnalbeDemo.getCount());
    }

    private static class AddDemo {
        private final AtomicInteger count = new AtomicInteger();
        private final ReentrantLock reentrantLock = new ReentrantLock();
        private final Semaphore semaphore = new Semaphore(4);
        private void add() {
            try {
                reentrantLock.lock();
                count.getAndIncrement();
                //semaphore.acquire(1);
            } finally {
                reentrantLock.unlock();
                semaphore.release();
                ConcurrentHashMap<Object, Object> objectObjectConcurrentHashMap = new ConcurrentHashMap<>();
                objectObjectConcurrentHashMap.size();
            }
        }

        int getCount() {
            return count.get();
        }
    }
}
